#include "sys.h"
#include "tcp_interface.h"
#include <stdio.h>
#include <string.h>
CNC_Coordinate_t System_ProgCoordinateOrigin={0,0,0};
CNC_Coordinate_t System_RefCoordinate = {0,0,0};

void (*TransaCallback)(int x,int y,int z)=CX_NULL;
void (*FeedSpeedCallback)(float feedspeed) = CX_NULL;
void (*CoolingControCallbackl)(int status) =CX_NULL;
void (*MainshaftControlCallback)(int status) = CX_NULL;
void (*MainshaftSpeedCallback)(float speed) = CX_NULL;
float system_expected_feed = 0;
float system_feed_rate = 1;

float system_feed_speed = 10;
float system_feed_speed_min = 10;
float system_feed_speed_max = 800;

float system_spindle_speed = 0;
float system_interplate_period_ms = 5;

float system_pulse_equivalent_x = 0.02f;
float system_pulse_equivalent_y = 0.02f;
float system_pulse_equivalent_z = 0.02f;

int system_cooling_status = 0;
int system_mainshaft_Status = 0;
extern char tcp_recv_buf[];
extern char tcp_send_buf[];
__attribute__((weak)) void ReadRefCoordinate(void)
{

}

__attribute__((weak)) void SaveRefCoordinate(void)
{

}

__attribute__((weak)) int CNC_GoToCoordinate(CNC_Coordinate_t *pTargetCoord)
{
    return 0;
}

__attribute__((weak)) int CNC_UpdateRefCoordinate(CNC_Coordinate_t *pDeltaCoor)
{
    System_RefCoordinate.x += pDeltaCoor->x;
    System_RefCoordinate.y += pDeltaCoor->y;
    System_RefCoordinate.z += pDeltaCoor->z;
    return 0;
}

__attribute__((weak)) int CNC_SetFeedSpeed(float feed)
{
    int ret;
    if((feed < system_feed_speed_min))
    {
        system_feed_speed = system_feed_speed_min;
        ret = -1;
    }
    else if(feed > system_feed_speed_max)
    {
        system_feed_speed = system_feed_speed_max;
        ret = 1;
    }
    else
    {
        system_feed_speed = feed;
        ret = 0;
    }
    if(FeedSpeedCallback != CX_NULL)
    {
        FeedSpeedCallback(feed);
    }
    return ret;
}

__attribute__((weak)) int CNC_SetMainshaftSpeed(float speed)
{
    system_spindle_speed = speed;
    if(MainshaftSpeedCallback != CX_NULL)
    {
        MainshaftSpeedCallback(speed);
    }
}

__attribute__((weak)) int CNC_CoolingControl(int status)
{
    system_cooling_status = status;
    if(CoolingControCallbackl != CX_NULL)
    {
        CoolingControCallbackl(status);
    }
}

__attribute__((weak)) int CNC_SpindleControl(int status)
{
    system_mainshaft_Status = status;
    if(MainshaftControlCallback != CX_NULL)
    {
        MainshaftControlCallback(status);
    }
}

int CNC_SetPulseEquivalent(int axis, float pulse_quivalent_value)
{
    int ret = 0;
    switch (axis)
    {
    case 0:system_pulse_equivalent_x = pulse_quivalent_value;break;
    case 1:system_pulse_equivalent_y = pulse_quivalent_value;break;
    case 2:system_pulse_equivalent_z = pulse_quivalent_value;break;
    
    default: ret = -1;
        break;
    }
    return ret;
}


// extern int clifd;
int CNC_Move(int dx,int dy, int dz)
{
    // System_ProgCoordinateOrigin.x += dx;
    // System_ProgCoordinateOrigin.y += dy;
    // System_ProgCoordinateOrigin.z += dz;
    System_RefCoordinate.x += dx;
    System_RefCoordinate.y += dy;
    System_RefCoordinate.z += dz;
    // CONVERT_PACKET(tcp_send_buf)->lenght = 14;
    // CONVERT_PACKET(tcp_send_buf)->type = TRANSACTION_TYPE_COORDIC;
    // memcpy((unsigned char *)(CONVERT_PACKET(tcp_recv_buf)->data),((unsigned char*)(&System_RefCoordinate)),12);
    // send(clifd,tcp_send_buf,CONVERT_PACKET(tcp_send_buf)->lenght,0);
    if(TransaCallback != CX_NULL)
    {
        TransaCallback(System_RefCoordinate.x,System_RefCoordinate.y,System_RefCoordinate.z);
    }


    return 0;
}

void TransactorRegister(void (*cb)(int,int,int))
{
    TransaCallback = cb;
}

void CoolingControlRegister(void (*cb)(int))
{
    CoolingControCallbackl = cb;
}

void MainshaftControlRegister(void (*cb)(int))
{
    MainshaftControlCallback = cb;
}

void MainshaftSpeedControlRegister(void (*cb)(float))
{
    MainshaftSpeedCallback=cb;
}

void FeedspeedControlRegister(void (*cb)(float))
{
    FeedSpeedCallback = cb;
}